package com.tenghu.core.dao.impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.tenghu.core.dao.IEmpDao;
import com.tenghu.core.model.Emp;
@Repository
public class EmpDaoImpl implements IEmpDao{
	
	@Autowired
	private SessionFactory sessionFactory;

	public void saveEmp(Emp emp) {
		sessionFactory.getCurrentSession().save(emp);
	}

	public List<Emp> queryEmpList() {
		Session session=sessionFactory.getCurrentSession();
		//创建Query对象
		Query query=session.createQuery("from Emp");
		return query.list();
	}

	public void updateEmp(Emp emp) {
		Session session=sessionFactory.getCurrentSession();
		session.update(emp);
	}

	public Emp queryEmpByName(String name) {
		Session session=sessionFactory.getCurrentSession();
		//创建Query对象
		//Query query=session.createQuery("from Emp where name=?");
		//query.setString(0, name);
		Query query=session.createQuery("from Emp where name=:name");
		query.setString("name", name);
		//获取查询结果
		List<Emp> empList=query.list();
		if(empList.size()>0)
			return empList.get(0);
		return null;
	}

	public void deleteEmp(Emp emp) {
		Session session=sessionFactory.getCurrentSession();
		session.delete(emp);
	}

}
